package com.bkgy.controller.frsaHistory.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.commom.model.BaseRangeObj;
import com.bkgy.commom.model.Temp;
import com.bkgy.controller.frsaHistory.entity.FrsaHistory;
import com.bkgy.controller.frsaHistory.entity.RealDataDic;
import com.bkgy.controller.frsaHistory.entity.dto.Cal1630Obj;
import com.bkgy.controller.frsaHistory.entity.dto.FrsaCharReq;
import com.bkgy.controller.frsaHistory.entity.dto.FrsaHistoryReq;
import com.bkgy.controller.frsaHistory.entity.dto.SteelFreRes;
import com.bkgy.controller.frsaHistory.mapper.IFrsaHistoryMapper;
import com.bkgy.controller.frsaHistory.mapper.RealDataDicDao;
import com.bkgy.controller.frsaHistory.service.IFrsaHistoryService;
import com.bkgy.controller.lzDaNew.entity.LzDaNew;
import com.bkgy.controller.lzDaNew.mapper.ILzDaNewMapper;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.DateUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

@Slf4j
@Service
@RequiredArgsConstructor
public class FrsaHistoryServiceImpl extends ServiceImpl<IFrsaHistoryMapper, FrsaHistory> implements IFrsaHistoryService {
    private final IFrsaHistoryMapper mapper;
    private final RealDataDicDao realDataDicDao;
    private final ILzDaNewMapper iLzDaNewMapper;

    public ResultData<FrsaHistory> findDataByPage(FrsaHistoryReq frsaHistory) {
        Integer pageIndex = frsaHistory.getPageIndex();
        Integer pageSize = frsaHistory.getPageSize();

        QueryWrapper<FrsaHistory> queryWrapper = new QueryWrapper<FrsaHistory>();
        queryWrapper.orderByDesc("MODIFY_TIME");
        Page<FrsaHistory> page = new Page<FrsaHistory>(pageIndex, pageSize);
        IPage<FrsaHistory> list = mapper.selectPage(page, queryWrapper);
        for (FrsaHistory his : list.getRecords()) {
            LzDaNew lzDaNew = iLzDaNewMapper.findDataByRollID(his.getRollId());
            if (lzDaNew != null) {
                his.setRollNo(lzDaNew.getRollNo());
                his.setPos(lzDaNew.getPos());
            }
        }

        return ResultData.ResultDataSuccess(list, (int) list.getTotal());
    }

    public ResultData<RealDataDic> getDicPropCode(RealDataDic realDataDic) {
        return ResultData.ResultDataSuccess(realDataDicDao.getDicPropCode(realDataDic));
    }

    @Override
    public ResultData<JSONObject> steelFre(BaseRangeObj obj) {
        String beginDate = obj.getBeginDate();
        String endDate = obj.getEndDate();
        List<Cal1630Obj> list = mapper.selectSteelFrsa(beginDate, endDate);
        //按钢种、宽度、厚度分组
        Map<String, List<Cal1630Obj>> steelMap = new HashMap<>();
        Map<String, List<Cal1630Obj>> widthMap = new HashMap<>();
        Map<String, List<Cal1630Obj>> thkMap = new HashMap<>();
        for (Cal1630Obj cal : list) {
            String steelgrade = cal.getSteelgrade();
            if (!steelMap.containsKey(steelgrade)) {
                List<Cal1630Obj> l = new ArrayList<>();
                l.add(cal);
                steelMap.put(steelgrade, l);
            } else {
                steelMap.get(steelgrade).add(cal);
            }
            String width = cal.getOutMatWidth();
            if (!widthMap.containsKey(width)) {
                List<Cal1630Obj> l = new ArrayList<>();
                l.add(cal);
                widthMap.put(width, l);
            } else {
                widthMap.get(width).add(cal);
            }
            String thk = cal.getOutMatThk();
            if (!thkMap.containsKey(thk)) {
                List<Cal1630Obj> l = new ArrayList<>();
                l.add(cal);
                thkMap.put(thk, l);
            } else {
                thkMap.get(thk).add(cal);
            }
        }

        JSONObject jsonObj = new JSONObject();
        String[] typeName = new String[]{"steelChar", "widthChar", "thkChar"};
        Map<String, List<Cal1630Obj>>[] arr = new Map[]{steelMap, widthMap, thkMap};
        for (int i = 0; i < arr.length; i++) {
            Map<String, List<Cal1630Obj>> map = arr[i];
            List<SteelFreRes> dataList = new ArrayList<>();
            for (Map.Entry<String, List<Cal1630Obj>> entry : map.entrySet()) {
                SteelFreRes res = new SteelFreRes();
                res.setTypeGroup(entry.getKey());
                List<Cal1630Obj> actList = entry.getValue();
                res.setActRoll(actList.size());
                int frsaRoll = 0;
                for (Cal1630Obj cal : actList) {
                    if (cal.getFrsaList().size() > 0) {
                        frsaRoll++;
                    }
                }
                res.setFrsaRoll(frsaRoll);
                res.setFreRate(BigDecimal.valueOf(res.getFrsaRoll() * 100).divide(BigDecimal.valueOf(res.getActRoll()), 2, RoundingMode.HALF_UP));
                dataList.add(res);
            }
            Collections.sort(dataList);
            jsonObj.put(typeName[i], dataList);
        }

        return ResultData.ResultDataSuccess(jsonObj);
    }

    @Override
    public ResultData<JSONObject> rollFreChar(FrsaCharReq obj) {
        List<Temp> list = mapper.selectRollData(obj.getBeginDate());
        List<String> xArr = new ArrayList<>();
        List<Integer> yArr = new ArrayList<>();
        for (Temp temp : list) {
            xArr.add(temp.getK());
            yArr.add(temp.getC());
        }
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("xArr", xArr);
        jsonObj.put("yArr", yArr);
        return ResultData.ResultDataSuccess(jsonObj);
    }

    @Override
    public ResultData<JSONObject> areaFreChar() {
        String beginDate = DateUtil.getDateToString(DateUtil.getCurrentZero(new Date()), "yyyy-MM-dd");
        List<Temp> list = mapper.selectAreaData(beginDate);
        List<String> xArr = new ArrayList<>();
        List<Integer> yArr = new ArrayList<>();
        for (Temp temp : list) {
            xArr.add(temp.getK());
            yArr.add(temp.getC());
        }

        JSONObject jsonObj = new JSONObject();
        jsonObj.put("xArr", xArr);
        jsonObj.put("yArr", yArr);
        return ResultData.ResultDataSuccess(jsonObj);
    }
}
